home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / x2ftp / msdos / source / ej-tsr / tsr.asm < prev    next >
Encoding:
Assembly Source File  |  1994-11-27  |  7.0 KB  |  278 lines

  1. CODE    SEGMENT BYTE PUBLIC 'CODE'
  2. ASSUME  CS:CODE,DS:CODE,ES:NOTHING,SS:NOTHING
  3.  
  4. ID      equ     128     ;the id character to find in the psp command tail
  5. VTBL    equ     90h     ;place in psp where vector table is stored
  6. MTBL    equ     (VTBL+400h) 
  7.  
  8. P00100  PROC
  9.         ORG     0100h
  10.  
  11. go:     JMP    start
  12.  
  13.         DB      8,8,8           ;hide the jump
  14.  
  15. help    DB     'Marks memory so TSRs in low memory can be removed.'
  16.         DB     13,10
  17.         DB     'Multiple marks can be used, LIFO.'
  18.         DB     13,10,'Syntax: tsr [i|r]',13,10
  19.         DB      9,'to Install or Remove TSRs',13,10
  20.         DB      'Send bugs to ejohnson@csn.org',13,10
  21.         DB      'Public Domain',13,10,1Ah,8,32,'$'
  22.  
  23.      
  24.  
  25. ;--------------------------------------------------------------------------
  26.  
  27.  
  28. start:  
  29.         mov     ax,ds:[2Ch]     ;release the environment memory block
  30.         mov     es,ax           ;good for both install & remove
  31.         mov     ah,049h
  32.         int     21h
  33.         xor     ax,ax
  34.         mov     ds:[2Ch],ax     ;clear env ptr in psp
  35.  
  36.         mov     ax,cs
  37.         mov     es,ax
  38.         mov     si,offset r0                    ;relocate code
  39.         mov     cx,offset r1- offset r0
  40.         mov     di,4000h
  41.         rep movsb
  42.  
  43.         ;jmp     4000h              ;to relocation
  44.         db      0E9h
  45.         dw      3E22h
  46.  
  47. ;---------------------------------------------------------------------------
  48.  
  49. r0:     mov     al,cs:[82h]
  50.  
  51.         cmp     al,'r'
  52.         jz      r
  53.         cmp     al,'i'
  54.         jz      i
  55.         
  56.         mov     dx,offset help
  57.         mov     ah,9h
  58.         int     21h
  59.         
  60. quit:   mov     ax,4C00h        ;end program
  61.         int     21h
  62.  
  63. r:      jmp     REMOVE
  64.  
  65. i:      jmp     INSTALL
  66.  
  67. ;--------------------------------------------------------------------------
  68.  
  69.  
  70. lmcb    dw      0       ;last mcb found, 0 => not started yet
  71. tblsz   dw      0       ;mcb table size
  72. mcb     dw      0       ;mcb of tsr's install invocation 
  73.  
  74.  
  75. ;--------------------------------------------------------------------------
  76.  
  77.                         
  78. MCBTBL  PROC    NEAR            ;store list of used mcbs at ds:di
  79.                                 ;last entry is 0000h
  80.                                 ;ax returns(paragraphs used for list)                
  81.  
  82.         push    cs
  83.         pop     ds
  84.                                                 
  85.         mov     ah,52h          ;get list of lists
  86.         int     21h
  87.         mov     ax,es:[bx-2]    ;ax = first MCB segment
  88.         mov     es,ax           ;es is segment of mcb
  89.         mov     lmcb,ax
  90.  
  91.         xor     cx,cx           ;count blocks stored
  92.         
  93.         mov     di,490h
  94.  
  95. agn:    mov     ax,lmcb
  96.         mov     ds:[di],ax      ;store mcb block address
  97.         inc     cx
  98.         add     di,2
  99.  
  100.         mov     dx,lmcb   ;????
  101.  
  102.         mov     es,ax
  103. again:  mov     al,es:[0]
  104.         cmp     al,'Z'
  105.         jz      done            ;j if no more mcbs
  106.  
  107.         mov     bx,es:[3]       ;get memory block size in paragraphs
  108.         inc     bx              ;add 1 for mcb
  109.         mov     ax,es           ;add this location
  110.         add     bx,ax
  111.         mov     es,bx           ;es points to next mcb
  112.  
  113.         mov     lmcb,bx         ;save mcb ??????????????????????
  114.         
  115.         mov     ax,es:[1]       ;is this mcb free?
  116.         or      ax,ax
  117.         jz      again           ;free mcb, so go find the next mcb
  118.  
  119.         mov     ax,es           ;don't save this programs mcb           
  120.         mov     bx,cs
  121.         dec     bx
  122.         cmp     ax,bx
  123.         jz      again
  124.  
  125.         mov     ax,es:[91h]     ;install block ?
  126.         cmp     ax,ID     
  127.         jnz     agn
  128.  
  129.         mov     ax,es           ;found 'INSTALL' mcb
  130.         mov     mcb,ax
  131.         jmp     agn             ;store block in table & continue
  132.  
  133. done:
  134.         xor     ax,ax           ;terminate list with 0h
  135.         mov     ds:[di],ax
  136.         inc     cx
  137.                                        
  138.         shr     cx,3            ;count up mcb table size in paragraphs
  139.         inc     cx
  140.         mov     tblsz,cx
  141.         ret
  142.                 
  143.  
  144. MCBTBL  ENDP
  145.  
  146. ;---------------------------------------------------------------------------
  147.  
  148. COMPARE PROC    NEAR
  149.  
  150. ;parameters
  151. ; in            ax = mcb segment to test
  152. ; out           clc = found, stc not found
  153. ;               ax unchanged
  154.  
  155.         mov     cx,mcb          ;es:di points to install mcb table
  156.         mov     es,cx
  157.         mov     di,4A0h
  158.  
  159. cag:    mov     cx,es:[di]      ;cx = table entry
  160.         or      cx,cx
  161.         jz      cdone           ;at 0000h end of table, so done
  162.  
  163.         cmp     ax,cx
  164.         jz      cfound          ;found ?
  165.  
  166.         add     di,2
  167.         jmp     cag
  168.  
  169.  
  170. cfound: clc
  171.         ret
  172. cdone:  stc
  173.         ret
  174.  
  175. COMPARE ENDP
  176.  
  177. ;--------------------------------------------------------------------------
  178.  
  179.  
  180. REMOVE  PROC    NEAR            ;remove back to last install
  181.  
  182.         call    MCBTBL   
  183.  
  184.         cli                     ;restore the vector table
  185.         mov     ax,mcb          ;source
  186.         or      ax,ax
  187.         jnz      ra            ;no install happened, so quit
  188. rb:     jmp     quit
  189. ra:     mov     ds,ax
  190.         mov     si,VTBL+10h
  191.         xor     ax,ax           ;destination
  192.         mov     es,ax
  193.         mov     di,ax
  194.         mov     cx,400h
  195.         rep movsb               ;move it
  196.         sti
  197.  
  198.         mov     si,490h         ;location of new mcb table ds:si
  199.         push    cs
  200.         pop     ds
  201.  
  202. ag:     mov     ax,ds:[si]
  203.         or      ax,ax
  204.         jz      rb              ;quit
  205.  
  206.  
  207.         call    COMPARE
  208.         jnc     keepit
  209.  
  210.         call    RELEASE
  211. keepit: add     si,2
  212.         jmp     ag
  213.  
  214.  
  215. REMOVE  ENDP
  216.  
  217.  
  218. ;------------------------------------------------------------------------
  219.  
  220.  
  221. RELEASE PROC    NEAR            ;release memory
  222.  
  223.         inc     ax              ;point to block, not mcb
  224.         
  225.         mov     bx,cs
  226.         cmp     ax,bx
  227.         jz      skip            ;don't release this program
  228.         
  229.         mov     es,ax
  230.         mov     ah,049h
  231.         int     21h
  232. skip:   ret
  233.  
  234. RELEASE ENDP
  235.  
  236.  
  237. ;--------------------------------------------------------------------------
  238.  
  239.  
  240.  
  241. INSTALL PROC    NEAR            ;save vectors & mcbs
  242.  
  243.         mov     ax,ID           ;load ID byte
  244.         mov     cs:[81h],ax     ;into PSP command tail
  245.  
  246.         xor     ax,ax           ;set source
  247.         mov     ds,ax
  248.         mov     si,ax
  249.         mov     ax,cs           ;set destination
  250.         mov     es,ax
  251.         mov     di,VTBL
  252.         cld
  253.         mov     cx,1024         ;vector table size
  254.         rep movsb               ;copy table
  255.         
  256.         call    MCBTBL
  257.  
  258.         mov     dx,tblsz        ;paragraphs
  259.         add     dx,40h-7h+10h
  260.  
  261.         MOV    ah,31h          
  262.     int    21h             ;TSR
  263. r1:
  264.  
  265.  
  266. INSTALL ENDP
  267.  
  268.  
  269. ;--------------------------------------------------------------------------
  270.  
  271. P00100  ENDP
  272.  
  273. CODE    ENDS
  274.         END     go
  275.  
  276.  
  277.  
  278.